﻿using MLSharp.Classification;

namespace MLSharp.Evaluation
{
	/// <summary>
	/// A result writer that outputs a <see cref="ConfusionGrid"/>.
	/// </summary>
	public class ConfusionGridResultWriter : IFileResultWriter
	{
		/// <summary>
		/// The logger.
		/// </summary>
		private log4net.ILog mLogger =
			log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		/// <summary>
		/// The name of the file being written to.
		/// </summary>
		public string OutputFileName { get; set; }

		/// <summary>
		/// Creates a writer with no output file specified. 
		/// </summary>
		public ConfusionGridResultWriter()
		{
			
		}

		/// <summary>
		/// Creates a writer with the specified output file name.
		/// </summary>
		/// <param name="outputFileName"></param>
		public ConfusionGridResultWriter(string outputFileName)
		{
			OutputFileName = outputFileName;
		}

		/// <summary>
		/// Writes the specified results.
		/// </summary>
		/// <param name="dataSet"></param>
		/// <param name="classificationResults"></param>
		public void ProcessResults(IDataSet dataSet, ClassificationResult[] classificationResults)
		{
			ConfusionGrid grid = new ConfusionGrid(dataSet, classificationResults);

			grid.SaveHtmlFile(OutputFileName);
		}

		/// <summary>
		/// When called, the writer should initialize/re-initialize.
		/// This is provided so that a single result writer can be used
		/// multiple times.
		/// </summary>
		public void Initialize()
		{
			mLogger.Debug("Writing output to " + OutputFileName);
		}

		/// <summary>
		/// Closes the underlying file stream.
		/// </summary>
		public void Close()
		{
			//The grid doesn't actually maintain a file stream...
		}
	}
}
